package com.biz.primus.app.rest.controller.product;

import com.biz.primus.app.rest.controller.base.RestBaseController;
import com.biz.primus.app.rest.feign.CommodityFeignClient;
import com.biz.primus.app.rest.feign.DepotFeignClient;
import com.biz.primus.app.rest.feign.ProductFeignClient;
import com.biz.primus.base.support.rest.vo.JsonResult;
import com.biz.primus.app.rest.vo.product.CategoryBrandRequestVo;
import com.biz.primus.app.rest.vo.product.CategoryBrandVo;
import com.biz.primus.base.vo.ProductListRestVo;
import com.biz.primus.base.global.GlobalParams;
import com.biz.primus.base.vo.IdNameVo;
import com.biz.primus.base.vo.PageResult;
import com.biz.primus.base.vo.ProductFieldVo;
import com.biz.primus.commodity.vo.frontend.ProductDetailRequestVo;
import com.biz.primus.commodity.vo.frontend.ProductDetailResult;
import com.biz.primus.commodity.vo.frontend.ProductRelevantDetail;
import com.biz.primus.commodity.vo.frontend.RecommendProductReqVo;
import com.biz.primus.search.vo.search.SearchProductRequestVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author jiangnan
 * @Description:
 */
@Controller
@RequestMapping("/product")
@Api("商品相关接口")
public class ProductController extends RestBaseController {


    @Autowired
    private CommodityFeignClient commodityFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private DepotFeignClient depotFeignClient;

    @ApiOperation(value = "1 分类品牌接口" +
            "根据分类id查询分类品牌页信息 categoryId为空时,返回第一个分类的品牌信息"
            , response = CategoryBrandVo.class)
    @GetMapping(value = "getCategoryBrandList")
    JsonResult getCategoryBrandList(GlobalParams gp, CategoryBrandRequestVo requestVo) {
        this.buildRequest(gp, requestVo);
        List<IdNameVo> categorys = commodityFeignClient.findAllCategoryForApp();
        if (requestVo.getCategoryId()==null){
            requestVo.setCategoryId(Long.valueOf(categorys.get(0).getId()));
        }
        List<IdNameVo> brands = commodityFeignClient.listBrandByCategoryId(requestVo.getCategoryId());
        return new JsonResult(new CategoryBrandVo(categorys, brands));
    }

    @ApiOperation(value = "2 搜索商品接口", response = ProductListRestVo.class)
    @PostMapping(value = "searchProduct")
    JsonResult searchProduct(GlobalParams gp,@RequestBody SearchProductRequestVo requestVo) {
        depotFeignClient.getByDepotCodeFRdb(gp.getDepotCode()).ifPresent((p)->{
            requestVo.setDepotLat(p.getDepotLatitude());
            requestVo.setDepotLon(p.getDepotLongitude());
        });
        this.buildRequest(gp, requestVo);
        PageResult<ProductListRestVo> respVo = productFeignClient.searchProduct(requestVo);
        return new JsonResult(respVo);
    }

    @ApiOperation(value = "3 商品详情接口", response = ProductDetailResult.class)
    @PostMapping(value = "productDetail")
    JsonResult productDetail(GlobalParams gp,@RequestBody ProductDetailRequestVo requestVo) {
        depotFeignClient.getByDepotCodeFRdb(gp.getDepotCode()).ifPresent((p)->{
            requestVo.setDepotLat(p.getDepotLatitude());
            requestVo.setDepotLon(p.getDepotLongitude());
        });
        this.buildRequest(gp, requestVo);
        ProductDetailResult respVo = productFeignClient.productDetail(requestVo);
        return new JsonResult(respVo);
    }

    @ApiOperation(value = "4 推荐商品接口", response = ProductListRestVo.class)
    @PostMapping(value = "recommendProducts")
    JsonResult recommendProducts(GlobalParams gp,@RequestBody RecommendProductReqVo requestVo) {
        depotFeignClient.getByDepotCodeFRdb(gp.getDepotCode()).ifPresent((p)->{
            requestVo.setDepotLat(p.getDepotLatitude());
            requestVo.setDepotLon(p.getDepotLongitude());
        });
        this.buildRequest(gp, requestVo);
        List<ProductListRestVo> respVo = productFeignClient.recommendProducts(requestVo);
        return new JsonResult(respVo);
    }

    @ApiOperation(value = "5 获取商品规格", response = ProductRelevantDetail.class)
    @PostMapping(value = "relevanceProducts")
    JsonResult relevanceProducts(GlobalParams gp,@RequestBody Long productId) {
        List<ProductRelevantDetail> relevant = commodityFeignClient.getRelevant(productId);
        return new JsonResult(relevant);
    }

    @ApiOperation(value = "6 分类获取筛选条件,注意 返回List的ProductFieldVo", response = ProductFieldVo.class)
    @PostMapping(value = "findFieldByCategoryId")
    JsonResult findFieldByCategoryId(GlobalParams gp,@RequestBody Long categoryId) {
        List<ProductFieldVo> respVo = commodityFeignClient.findFieldByCategoryId(categoryId);
        return new JsonResult(respVo);
    }

}
